package com.xunyin.modules.members.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.common.utils.QueryHelpPlus;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.members.domain.TMembers;
import com.xunyin.modules.members.param.TMembersQueryParam;
import com.xunyin.modules.members.service.TMembersService;
import com.xunyin.modules.members.service.dto.TMembersQueryCriteria;
import com.xunyin.modules.members.service.mapper.TMembersMapper;
import com.xunyin.modules.members.vo.TMembersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2022-09-22
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TMembersServiceImpl extends BaseServiceImpl<TMembersMapper, TMembers> implements TMembersService {

    @Autowired
    private IGenerator generator;

    @Override
    public Map<String, Object> queryAll(TMembersQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TMembers> page = new PageInfo<>(queryAll(criteria));
        //List<TStoreRoomDto> tStoreRoomDto = new ArrayList<>();
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<TMembers> queryAll(TMembersQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(TMembers.class, criteria));
    }

    /**
     * 获取会员设置列表
     *
     * @return List<TStoreVo>
     */
    @Override
    public List<TMembersVo> listMemberSet(TMembersQueryParam queryParam) {
        LambdaQueryWrapper<TMembers> wrapper = new LambdaQueryWrapper<>();
        if (queryParam.getMainId() != null) {
            wrapper.eq(TMembers::getMainId, queryParam.getMainId());
        }
        List<TMembersVo> list = generator.convert(baseMapper.selectList(wrapper), TMembersVo.class);
        return list;
    }

    /**
     * 根据主体Id、会员等级获取会员等级信息
     *
     * @return List<TStoreVo>
     */
    @Override
    public TMembers selectByMainId(Integer mainId) {
        LambdaQueryWrapper<TMembers> wrapper = new LambdaQueryWrapper<>();
        if (mainId != null) {
            wrapper.eq(TMembers::getMainId, mainId);
        }
        wrapper.eq(TMembers::getMembersLevel, 2);
        TMembers tMembers = baseMapper.selectOne(wrapper);
        return tMembers;
    }
}
